package cn.javabb.admin.controller.sys;

import cn.javabb.common.model.ResponseModel;
import cn.javabb.common.utils.ListUtils;
import cn.javabb.core.base.BaseController;
import cn.javabb.core.entity.biz.NavDO;
import cn.javabb.core.service.biz.NavService;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author QINB imqinbao@163.com
 * @CreateDate 2018/11/27/027 15:19
 * @Since V1.0
 */
@Controller
@RequestMapping("/admin/nav")
public class NavAdminController extends BaseController {
    @Autowired
    private NavService navService;
    /**
     * 菜单列表页面
     * @author QINB
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String toListPage(Model model) {
        return "admin/biz/nav/nav";
    }
    /**
     * 菜单保存页面
     * @author QINB
     * @param model
     * @return
     */
    @GetMapping("/save")
    public String toSavePage(Model model) {
        return "admin/biz/nav/navSave";
    }

    /**
     * 查询导航列表
     */
    @ResponseBody
    @GetMapping("/")
    public Object list() {
        String page = getPage();
        String size = getSize();
        String navName = getParamValue("navName");

        System.out.println(navName);
        NavDO nav = new NavDO();
        if(StringUtils.isNotBlank(navName)){
            nav.setNavName(navName);
        }
        Page navPage = navService.queryPageByWhere(page, size, nav);
        return ResponseModel.table(navPage);
    }

    /**
     * 更新
     * @param nav
     * @return
     */
    @ResponseBody
    @PostMapping("/")
    public Object saveOrUpdate(NavDO nav){
        int n = 0;
        if(null == nav.getNavId()){
            if(null == nav.getState()){nav.setState(1);}
            n = navService.insertSelective(nav);
        }else{
            n = navService.updateSelective(nav);
        }
        if(n>0){
            return ResponseModel.Success("保存成功");
        }
        return ResponseModel.Failure("保存失败");

    }

    /**
     * 删除
     * @param navId
     * @return
     */
    @ResponseBody
    @DeleteMapping("/{navId}")
    public Object delete(@PathVariable String navId){
        if(StringUtils.isBlank(navId)){
            return ResponseModel.Failure("参数错误");
        }
        List<String> idList = ListUtils.stringToListString(navId);
        int num = navService.deleteByIds(NavDO.class, "navId", idList);
        if(num>0){
            return ResponseModel.Success("删除成功");
        }
        return ResponseModel.Failure("删除失败");
    }

    /**
     * 更新状态
     * @param navId
     * @param state
     * @return
     */
    @ResponseBody
    @PostMapping("/updateState")
    public Object updateState(Integer navId,Integer state){
        if(navService.updateState(navId,state)){
            return ResponseModel.Success("状态更新成功");
        }
        return ResponseModel.Failure("状态更新失败");
    }
}
